
<template>
  <basic-container>
    <avue-crud
      ref="crud"
      :page.sync="page"
      :data="tableData"
      :permission="permissionList"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="beforeOpen"
      @on-load="getList"
      @row-update="handleUpdate"
      @row-save="handleSave"
      @search-reset="searchReset"
      @search-change="searchChange"
    >
      <template slot="menuLeft">
        <el-button
          v-if="permissions.sys_dict_del"
          class="filter-item"
          type="primary"
          size="small"
          icon="el-icon-refresh-left"
          @click="handleRefreshCache"
        >缓存
        </el-button>
      </template>
      <template
        slot="menu"
        slot-scope="scope"
      >
        <el-button
          v-if="permissions.sys_dict_edit && scope.row.systemFlag !== '1'"
          type="text"
          size="small"
          icon="el-icon-menu"
          @click="handleEdit(scope.row,scope.index)"
        >编辑
        </el-button>
        <el-button
          v-if="permissions.sys_dict_del && scope.row.systemFlag !== '1'"
          type="text"
          size="small"
          icon="el-icon-menu"
          @click="rowDel(scope.row)"
        >删除
        </el-button>
        <el-button
          v-if="permissions.sys_dict_add"
          type="text"
          size="small"
          icon="el-icon-menu"
          @click="handleItem(scope.row,scope.index)"
        >字典项
        </el-button>
      </template>
    </avue-crud>
    <el-dialog
      :visible.sync="dialogFormVisible"
      title="字典项管理"
      width="90%"
      @close="dictItemVisible"
    >
      <avue-crud
        ref="crudItem"
        v-model="form"
        :page.sync="itemPage"
        :data="tableDictItemData"
        :permission="permissionList"
        :before-open="handleBeforeOpen"
        :option="tableDictItemOption"
        @size-change="itemSizeChange"
        @current-change="itemCurrentChange"
        @row-update="handleItemUpdate"
        @row-save="handleItemSave"
        @row-del="rowItemDel"
      />
    </el-dialog>
  </basic-container>
</template>

<script>
import {
  addItemObj,
  addObj,
  delItemObj,
  delObj,
  fetchItemList,
  fetchList,
  putItemObj,
  putObj,
  refreshCache,
} from '@/api/admin/dict';
import { tableDictItemOption, tableOption } from '@/const/crud/admin/dict';
import { mapGetters } from 'vuex';
import crudMixins from '@/mixins/crud';
export default {
  name: 'Dict',
  mixins: [crudMixins],
  data() {
    return {
      form: {
        dictType: undefined,
        dictId: undefined,
      },
      dictType: undefined,
      dictId: undefined,
      dialogFormVisible: false,
      tableDictItemData: [],
      itemPage: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
      },
      tableOption: tableOption,
      tableDictItemOption: tableDictItemOption,
    };
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.sys_dict_add, false),
        // delBtn: this.vaildData(this.permissions.sys_dict_del, false),
        // editBtn: this.vaildData(this.permissions.sys_dict_edit, false),
      };
    },
  },
  methods: {
    //======字典表格相关=====
    getList(page = this.page, params = this.searchForm) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize,
            descs: 'create_time',
          },
          params,
          this.searchForm
        )
      ).then((response) => {
        this.tableData = response.data.data.records;
        this.page.total = response.data.data.total;
        this.tableLoading = false;
      });
    },
    rowDel: function (row) {
      this.$confirm(
        '是否确认删除数据类型为"' + row.dictType + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(function () {
          return delObj(row);
        })
        .then(() => {
          this.getList(this.page);
          this.$message.success('删除成功');
        })
        .catch(function () {});
    },
    handleEdit(row, index) {
      this.$refs.crud.rowEdit(row, index);
    },
    handleUpdate: function (row, index, done) {
      putObj(row).then(() => {
        this.$message.success('修改成功');
        this.getList(this.page);
        done();
      });
    },
    handleSave: function (row, done) {
      addObj(row).then(() => {
        this.$message.success('添加成功');
        this.getList(this.page);
        done();
      });
    },
    beforeOpen(show, type) {
      window.boxType = type;
      show();
    },
    //======字典项表格相关=====
    dictItemVisible: function () {
      this.dialogFormVisible = false;
      this.itemPage.currentPage = 1;
    },
    handleItem: function (row) {
      this.dictId = row.id;
      this.dictType = row.dictType;
      this.getDictItemList();
    },
    getDictItemList() {
      this.dialogFormVisible = true;
      fetchItemList(
        Object.assign(
          {
            current: this.itemPage.currentPage,
            size: this.itemPage.pageSize,
          },
          { dictId: this.dictId }
        )
      ).then((response) => {
        this.tableDictItemData = response.data.data.records;
        this.itemPage.total = response.data.data.total;
      });
    },
    handleBeforeOpen(done) {
      this.form.dictType = this.dictType;
      this.form.dictId = this.dictId;
      done();
    },
    handleItemSave: function (row, done) {
      addItemObj(row).then(() => {
        this.$message.success('添加成功');
        this.getDictItemList();
        done();
      });
    },
    handleItemUpdate: function (row, index, done) {
      putItemObj(row).then(() => {
        this.$message.success('修改成功');
        this.getDictItemList();
        done();
      });
    },
    itemSizeChange(pageSize) {
      this.itemPage.pageSize = pageSize;
      this.getDictItemList();
    },
    itemCurrentChange(current) {
      this.itemPage.currentPage = current;
      this.getDictItemList();
    },
    rowItemDel: function (row) {
      this.$confirm('是否确认删除数据为"' + row.label + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delItemObj(row.id);
        })
        .then(() => {
          this.getDictItemList();
          this.$message.success('删除成功');
        })
        .catch(function () {});
    },
    handleRefreshCache: function () {
      refreshCache()
        .then(() => {
          this.$message.success('同步成功');
        })
        .catch(function () {});
    },
  },
};
</script>

<style lang="scss" scoped>
</style>

